package com.jackiechancinema.ssm.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.jackiechancinema.ssm.dao.CommoditDao;
import com.jackiechancinema.ssm.dao.OrderDao;
import com.jackiechancinema.ssm.dto.CommoditExcelDTO;
import com.jackiechancinema.ssm.dto.OrderExcelDto;
import com.jackiechancinema.ssm.entity.Commodit;
import com.jackiechancinema.ssm.entity.Order;
import com.jackiechancinema.ssm.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommoditReadListener extends AnalysisEventListener<CommoditExcelDTO>{
    private List<Commodit> list = new ArrayList<>();
    @Autowired
    private CommoditDao commoditDao;
    //获取读取的每一行对应的对象
    @Override
    public void invoke(CommoditExcelDTO dto, AnalysisContext analysisContext) {
        System.out.println("1");
        //实现属性的赋值
        Commodit commodit= BeanUtil.copyProperty(Commodit.class,dto,dto.getClass().getDeclaredFields());

        list.add(commodit);
    }
    //整个读取结束
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println(list);
        commoditDao.insertBatch(list);
        list.clear();

    }
}
